//
//  YRStatusCell.swift
//  YR 微博
//
//  Created by cyr on 15/10/17.
//  Copyright © 2015年 cyr. All rights reserved.
//

import UIKit

 let statusCellControlMargin: CGFloat = 8
/// 微博 cell 的标示符枚举
enum StatusCellIdentifier: String {
    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    
    static func cellId(status: YRStatus) -> String {
        return status.retweeted_status == nil ? StatusCellIdentifier.NormalCell.rawValue : StatusCellIdentifier.ForwardCell.rawValue
    }
    
}

/// 微博 cell
class YRStatusCell: UITableViewCell {
    
    /// 微博数据模型
    var status: YRStatus? {
        didSet{
            topView.status = status
            
            contentLabel.text = status?.text ?? ""
            
            pictureView.status = status
            
            pictureWidthCons?.constant = pictureView.bounds.size.width
            pictureHeightCons?.constant = pictureView.bounds.size.height
            pictureTopCons?.constant = (pictureView.bounds.size.height == 0) ? 0 : statusCellControlMargin
       }
    }
 /// 图片宽度约束
    var pictureWidthCons: NSLayoutConstraint?
    
 /// 图片高度约束
    var pictureHeightCons: NSLayoutConstraint?
    
 /// 图片顶部约束
    var pictureTopCons: NSLayoutConstraint?
    
    
 /// 计算行高
    func rowHeight(status: YRStatus) -> CGFloat {
        self.status = status
        
        layoutIfNeeded()
        
        return CGRectGetMaxY(bottonView.frame)
    }
    
   // MARK: - 懒加载控件
    private lazy var topView :YRTopView = YRTopView()
    lazy var contentLabel: UILabel = {
        let label = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        label.numberOfLines = 0
        
        // 设置文本折行的宽度
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * statusCellControlMargin
        return label
    }()
    
    lazy var pictureView: YRStatusPictureView = YRStatusPictureView()
    lazy var bottonView: YRBottonView = YRBottonView()
    
    
   // MARK: - 界面设置
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
  
    func test1(){
    
    print(pictureView)
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
    设置界面
    */
    func setupUI() {
        // 添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottonView)
     
        
        //自动布局
        // 顶部视图
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        
        // 内容标签
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: statusCellControlMargin, y: statusCellControlMargin))
        
        // 宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -16))
        
//   
        
        
        // 底部约束
        bottonView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -statusCellControlMargin, y: statusCellControlMargin))
//        contentView.addConstraint(NSLayoutConstraint(item: bottonView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        
        
    }

}
